package com.example.blog.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.DigestUtils;

import java.util.UUID;

@Slf4j
public class SecurityUtils {
    public static String encrypt(String password) {
        String salt= UUID.randomUUID().toString().replace("-","");
        String securityPassword= DigestUtils.md5DigestAsHex((salt+password).getBytes());
        String finalPassword=salt+securityPassword;
        return finalPassword;
    }

    public static boolean verify(String inputPassword, String sqlPassword) {
        if(sqlPassword==null||sqlPassword.length()!=64){
            log.error("数据库中的密码格式不对");
            return false;
        }
        String salt=sqlPassword.substring(0,32);
        String secretPassword=DigestUtils.md5DigestAsHex((salt+inputPassword).getBytes());
        return sqlPassword.equals(salt+secretPassword);
    }
}
